/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-06-26 18:55:04
 * @LastEditTime: 2024-07-07 15:11:24
 * @FilePath: /nest_study/web/src/views/system/dict/components/info.tsx
 */
/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-06-23 12:37:05
 * @LastEditTime: 2024-06-25 20:58:59
 * @FilePath: /nest_study/web/src/views/system/role/components/info.tsx
 */

import DrawerWrapper from "@/components/drawer_wrapper"
import { Form, Input, InputNumber, message, Switch } from "antd"
import { TDicTypeInfo } from "../index.type";
import { useState } from "react";
import { addInfo, updateInfo } from "@/api/system/dict/dict_type";
import { rules } from "./rules";

const ShowInfo = (props: any) => {
    let { info } = props;
    let initForm: TDicTypeInfo = {
        name: '',
        code: '',
        sort: 0,
        status: '0',
        remark: '',
    }
    const [form] = Form.useForm<any>();
    const [infoForm, _setInfoForm] = useState<TDicTypeInfo>({ ...initForm, ...info });
    //提交
    const submit = async () => {
        form.validateFields().then(async values => {
            let info = { ...infoForm, ...values };
            if (info.id) {
                await updateInfo(info);
            } else {
                await addInfo(info);
            }
            message.success('操作成功');
            form.resetFields();
            props.submit();
        }).catch(info => {
            console.log('Validate Failed - ', info);
        });
    }
    return (
        <DrawerWrapper
            title="字典类型信息"
            close={() => props.close()}
            submit={() => submit()}
        >
            <Form
                form={form}
                name="info"
                labelCol={{ span: 5 }}
                wrapperCol={{ span: 17 }}
                initialValues={{ remember: false }}
                onFinish={submit}
                autoComplete="off"
            >
                <Form.Item label="字典编码" initialValue={infoForm.code} name="code" rules={rules.code}>
                    <Input allowClear disabled={infoForm?.id ? true : false} placeholder="请输入字典编码" />
                </Form.Item>
                <Form.Item label="字典名称" initialValue={infoForm.name} name="name" rules={rules.name}>
                    <Input allowClear placeholder="请输入字典名称" />
                </Form.Item>
                <Form.Item label="状态" initialValue={infoForm.status} name="status">
                    <Switch checked={infoForm.status == '0' ? true : false} onChange={(e) => {form.setFieldValue('status',e ? '0' : '1');_setInfoForm({ ...infoForm, status: e ? '0' : '1' })}} checkedChildren="启用" unCheckedChildren="关闭" />
                </Form.Item>
                <Form.Item label="排序" initialValue={infoForm.sort} name="sort" rules={rules.sort}>
                    <InputNumber min={0} precision={0} style={{ width: '100%' }} placeholder="请输入排序" />
                </Form.Item>
                <Form.Item label="备注" initialValue={infoForm.remark} name="remark">
                    <Input.TextArea placeholder="请输入备注" />
                </Form.Item>
            </Form>
        </DrawerWrapper>
    )
}

export default ShowInfo